package io.babystar.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;
import io.babystar.modules.business.dao.CWCategoryDao;
import io.babystar.modules.business.dao.CWOrderDao;
import io.babystar.modules.business.dao.CWRefundDao;
import io.babystar.modules.business.dao.StudentDao;
import io.babystar.modules.business.entity.CWRefundEntity;
import io.babystar.modules.business.service.CWRefundService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("cwRefundService")
public class CWRefundServiceImpl extends ServiceImpl<CWRefundDao, CWRefundEntity> implements CWRefundService {
    @Autowired
    CWCategoryDao cwCategoryDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    CWOrderDao cwOrderDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        String refundStatus = (String) params.get("refundStatus");
        String createStartDate = (String) params.get("createStartDate");
        String createEntDate = (String) params.get("createEntDate");
        IPage<CWRefundEntity> page = this.page(
                new Query<CWRefundEntity>().getPage(params),
                new QueryWrapper<CWRefundEntity>()
                        .eq("del_flag",0)
                        .eq(StringUtils.isNotBlank(orderId),"order_id",orderId)
                        .eq(StringUtils.isNotBlank(refundStatus),"refund_status",refundStatus)
                        .gt(StringUtils.isNotBlank(createStartDate),"create_at",createStartDate)
                        .le(StringUtils.isNotBlank(createEntDate),"create_at",createEntDate)
                        .orderByDesc("create_at")
        );
        List<CWRefundEntity> list = page.getRecords();
        list.stream().forEach(x -> {
            x.setCategoryName(cwCategoryDao.selectById(x.getCategoryId()).getCategory());
            x.setStudentName(studentDao.selectById(x.getStudentId()).getStudentName());
            //x.setAmountPaid(cwOrderDao.selectById(x.getOrderId()).getAmountPaid());
        });

        return new PageUtils(page);
    }

}